package x.y.z;

public class LeetCode_0875 {
    public int minEatingSpeed(int[] piles, int h) {
        int left = 1;
        int right = 1000000000 + 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (f(piles, mid) == h) {
                right = mid;
            } else if (f(piles, mid) > h) {
                left = mid + 1;
            } else if (f(piles, mid) < h) {
                right = mid;
            }
        }
        return left;
    }

    public int f(int[] piles, int x) {
        int hours = 0;
        for (int pile : piles) {
            hours += pile / x;
            if (pile % x > 0) {
                hours++;
            }
        }
        return hours;
    }
}
